﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FileCopy
{
    public partial class f_MainForm : Form
    {
        public f_MainForm()
        {
            InitializeComponent();
            #region
            //f_Item item = new f_Item() { Name = "任务-01", Filter = "*.txt", IncludeSubdirectories = false, DirectoryPath = "D:\\Data", DelOld = true };
            //item.DestPaths = new f_Directory[] 
            //{           
            //    new f_Directory("D:\\Data1"){ StartTime = "08:00:00", EndTime = "19:00:00", Enabled = true, Filters = new f_Filter[]
            //    {
            //        new f_Filter("GB_GET"),
            //        new f_Filter("GB_POST"),
            //    }},
            //    new f_Directory("D:\\Data2"){ StartTime = "08:00:00", EndTime = "19:00:00", Enabled = true, Filters = new f_Filter[]
            //    {
            //        new f_Filter("GB_GET"),
            //        new f_Filter("GB_POST"),
            //        new f_Filter("GB_ATTACH"),
            //    }}
            //};
            //try
            //{
            //    string info = SerialiazeHelper.XmlSerialiaze(new List<f_Item>() { item }, Encoding.UTF8);
            //    FileHelper.WriteToTxt(Application.StartupPath + "\\Config.xml", info, true);
            //}
            //catch (Exception ex)
            //{
            //}
            #endregion
        }

        private void f_MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                Config.Init();
                resetTreeList();
            }
            catch (Exception ex)
            {
                ExceptionManager.TraceWrite(ex.Message, "初始化加载...");
            }
        }

        private void listBoxMain_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index != -1)
            {
                Graphics g = e.Graphics;
                g.FillRectangle(new SolidBrush(Color.White), e.Bounds);
                f_Directory dir = listBoxMain.Items[e.Index] as f_Directory;
                Image image;
                if (dir.Enabled)
                    image = Resources.Flag_redHS;
                else
                    image = Resources.Flag_greenHS;
                g.DrawImage(image, 0, e.Bounds.Height * e.Index, 16, 16);
                g.DrawString(dir.ToString(), new Font("宋体", 10), new SolidBrush(Color.Black), e.Bounds.Left + 16, e.Bounds.Top, StringFormat.GenericTypographic);
            }
        }

        private void resetTreeList()
        {
            TreeNode rootNode = new TreeNode("根节点");
            foreach (f_Item dir in Config.Items)
            {
                TreeNode node = new TreeNode(dir.Name) { Tag = dir };
                rootNode.Nodes.Add(node);
            }
            this.treeViewDirectoryList.Nodes.Add(rootNode);
            if (rootNode.Nodes.Count > 0)
                this.treeViewDirectoryList.SelectedNode = this.treeViewDirectoryList.Nodes[0].Nodes[0];
            this.treeViewDirectoryList.ExpandAll();
        }

        private TreeNode selectedNode;
        private void treeViewDirectoryList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = treeViewDirectoryList.SelectedNode;
            if (node.Level == 1)
            {
                if (node != selectedNode)
                {
                    selectedNode = node;
                    editItemConfig(node);
                }
            }
        }

        private void editItemConfig(TreeNode node)
        {
            f_Item item = (f_Item)node.Tag;
            btnRun.Text = !item.Enabled ? "开启" : "关闭";
            this.tName.Text = item.Name;
            this.tSrcFileDir.Text = item.DirectoryPath;
            this.checkBoxDelOld.Checked = item.DelOld;
            this.checkBoxIncludeSubdirectories.Checked = item.IncludeSubdirectories;
            this.tFilter.Text = item.Filter;
            resetItemDestPath(item.DestPaths);
        }

        private void resetItemDestPath(f_Directory[] dirs)
        {
            this.listBoxMain.Items.Clear();
            this.listBoxMain.Items.AddRange(dirs);
        }

        private bool saveItemConfig()
        {
            if (selectedNode != null)
            {
                f_Item item = (f_Item)selectedNode.Tag;
                if (string.IsNullOrEmpty(this.tName.Text) || string.IsNullOrEmpty(this.tSrcFileDir.Text) || this.listBoxMain.Items.Count == 0)
                {
                    MessageBox.Show("未正确配置选项...", "提示");
                    return false;
                }
                item.Name = this.tName.Text;
                item.DirectoryPath = this.tSrcFileDir.Text;
                item.DelOld = this.checkBoxDelOld.Checked;
                item.IncludeSubdirectories = this.checkBoxIncludeSubdirectories.Checked;
                item.Filter = this.tFilter.Text;
                return true;
            }
            MessageBox.Show("未选中任何节点...", "提示");
            return false;
        }

        private void f_MainForm_Deactivate(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                ShowInTaskbar = false;
                Hide();
            }
        }

        private void notifyIconMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Visible = true;
            if (WindowState == FormWindowState.Minimized)
            {
                ShowInTaskbar = true;
                WindowState = FormWindowState.Normal;
            }
            BringToFront();
        }

        private void btnGetSrcDir_Click(object sender, EventArgs e)
        {
            if (selectedNode != null)
            {
                using (FolderBrowserDialog fbd = new FolderBrowserDialog() { ShowNewFolderButton = true, RootFolder = Environment.SpecialFolder.DesktopDirectory })
                {
                    if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        tSrcFileDir.Text = fbd.SelectedPath;
                    }
                }
            }
        }

        private void listBoxMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            modifyItem();
        }

        private void iModify_Click(object sender, EventArgs e)
        {
            modifyItem();
        }

        private void modifyItem()
        {
            object item = listBoxMain.SelectedItem;
            if (item != null)
            {
                f_Directory dir = item as f_Directory;
                using (f_DirForm df = new f_DirForm(dir))
                {
                    if (df.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        editItemConfig(selectedNode);
                    }
                }
            }
        }

        private void iAdd_Click(object sender, EventArgs e)
        {
            using (f_DirForm df = new f_DirForm(selectedNode))
            {
                if (df.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    editItemConfig(selectedNode);
                }
            }
        }

        private void iDel_Click(object sender, EventArgs e)
        {
            object item = listBoxMain.SelectedItem;
            if (item != null)
            {
                f_Directory dir = item as f_Directory;
                f_Item val = (f_Item)selectedNode.Tag;
                val.RemoveDestPath(dir);
                editItemConfig(selectedNode);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (saveItemConfig() && Config.Save())
                MessageBox.Show("保存完成...", "提示");
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            if (selectedNode != null)
            {
                f_Item item = (f_Item)selectedNode.Tag;
                item.Enabled = item.IsValid && !item.Enabled;
                btnRun.Text = !item.Enabled ? "开启" : "关闭";
            }
            else
                MessageBox.Show("未选中任何节点...", "提示");
        }
    }
}
